package kaindorfcompany.webshop.productmanagement.frontend.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kaindorfcompany.webshop.productmanagement.backend.access.DbAccess;
import kaindorfcompany.webshop.productmanagement.backend.data.Ingredient;
import kaindorfcompany.webshop.productmanagement.backend.exceptions.DbAccessFailureException;
import kaindorfcompany.webshop.productmanagement.frontend.servlets.util.ServletUtil;

/**
 * Servlet implementation class IngredientOverviewServlet
 */
@WebServlet("/admin/ingredients")
public class IngredientOverviewServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public IngredientOverviewServlet() {
    }

    /**
     * Show an overview for the ingredigents
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            List<Ingredient> ingredients = DbAccess.getInstance().getIngredients(true);
            request.setAttribute("ingredients", ingredients);
            ServletUtil.forwardToView(request, response, "showIngredients");
        } catch (DbAccessFailureException ex) {
            ServletUtil.showGeneralError(ex.getMessage(), request, response);
            log(null, ex);
            return;
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}
